{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "26d6da9a",
   "metadata": {},
   "source": [
    "管道提示词可以将多个提示组合在一起。当您想要重复使用部分提示时，这会很有用。这可以通过 PipelinePrompt 来完成。\n",
    "\n",
    "PipelinePrompt 由两个主要部分组成：\n",
    "- 最终提示：返回的最终提示\n",
    "- 管道提示：元组列表，由字符串名称和提示模板组成。每个提示模板将被格式化，然后作为具有相同名称的变量传递到未来的提示模板。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4dee4ae9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts.pipeline import PipelinePromptTemplate\n",
    "from langchain.prompts.prompt import PromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0f1b4544",
   "metadata": {},
   "outputs": [],
   "source": [
    "full_template = \"\"\"{introduction}\n",
    "\n",
    "{example}\n",
    "\n",
    "{start}\"\"\"\n",
    "full_prompt = PromptTemplate.from_template(full_template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ea377604",
   "metadata": {},
   "outputs": [],
   "source": [
    "introduction_template = \"\"\"你正在冒充{person}。\"\"\"\n",
    "introduction_prompt = PromptTemplate.from_template(introduction_template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "372e37c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "example_template = \"\"\"\n",
    "下面是一个交互示例：\n",
    "\n",
    "Q：{example_q}\n",
    "A：{example_a}\"\"\"\n",
    "example_prompt = PromptTemplate.from_template(example_template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3551fb5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "start_template = \"\"\"现在正式开始！\n",
    "\n",
    "Q：{input}\n",
    "A：\"\"\"\n",
    "start_prompt = PromptTemplate.from_template(start_template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6d01f02c",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_prompts = [\n",
    "    (\"introduction\", introduction_prompt),\n",
    "    (\"example\", example_prompt),\n",
    "    (\"start\", start_prompt),\n",
    "]\n",
    "pipeline_prompt = PipelinePromptTemplate(\n",
    "    final_prompt=full_prompt, pipeline_prompts=input_prompts\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4ac97898",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['input', 'person', 'example_q', 'example_a']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipeline_prompt.input_variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "372c01c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You are impersonating Elon Musk.\n",
      "\n",
      "\n",
      "下面是一个交互示例：\n",
      "\n",
      "Q：你最喜欢什么车？\n",
      "A：Tesla\n",
      "\n",
      "现在正式开始！\n",
      "\n",
      "Q：您最喜欢的社交媒体网站是什么?\n",
      "A：\n"
     ]
    }
   ],
   "source": [
    "print(\n",
    "    pipeline_prompt.format(\n",
    "        person=\"Elon Musk\",\n",
    "        example_q=\"你最喜欢什么车？\",\n",
    "        example_a=\"Tesla\",\n",
    "        input=\"您最喜欢的社交媒体网站是什么?\",\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e38c1b3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Twitter'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_openai import ChatOpenAI\n",
    "openai_api_key = \"EMPTY\"\n",
    "openai_api_base = \"http://127.0.0.1:1234/v1\"\n",
    "chat = ChatOpenAI(\n",
    "    openai_api_key=openai_api_key,\n",
    "    openai_api_base=openai_api_base,\n",
    "    temperature=0.7,\n",
    ")\n",
    "\n",
    "output_parser = StrOutputParser()\n",
    "\n",
    "chain = pipeline_prompt | chat | output_parser\n",
    "\n",
    "chain.invoke({\n",
    "    \"input\":\"您最喜欢的社交媒体网站是什么\",\n",
    "    \"person\":\"Elon Musk\",\n",
    "    \"example_q\":\"你最喜欢什么车？\",\n",
    "    \"example_a\":\"Tesla\",\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "378c5d9c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
